#!bin/bash

#
# Installation of NVIDIA graphic driver.
# 
# Target platform: Based on Ubuntu(Ubuntu Kylin/Ubuntu Desktop/Linux Mint/ZorinOS/DeepinOS), include graphical interface, and not applicable to any server version.
# Create date: 2019/06/25
# Create user: SteveJrong
# File version: 1.0.0

# Import some shells.
. common/common.sh

# ######################### start of variables #########################
BLACKLIST_CONTENT='blacklist vga16fb\n
blacklist nouveau\n
blacklist rivafb\n
blacklist rivatv\n
blacklist nvidiafb'
NVIDIA_GRAPHIC_DRIVER_VERSION=430
# ######################### end of variables #########################

# ######################### start of functions #########################
# ######################### end of functions #########################

# ######################### start of install NVIDIA graphic driver #########################
echo '------------------------- Starting install NVIDIA graphic driver.'
echo '------------------------- Verify nouveau driver status.'
nouveauDriver=`lsmod | grep nouveau`
if [ -z "$nouveauDriver" ]; then
    # No output, the nouveau driver was successfully disabled.
    echo '------------------------- Ok, Nouveau driver is disabled.'
else
    # Output some content, the driver has not been disabled.
    echo '------------------------- Nouveau driver is enabled, needs to be disabled first.'

    echo '------------------------- Backup blacklist file.'
    sudo chmod 777 /etc/modprobe.d/blacklist.conf
    sudo cp /etc/modprobe.d/blacklist.conf /etc/modprobe.d/blacklist.conf.bak
    echo -e '------------------------- Done.\n'
    sleep 5

    echo '------------------------- Edit blacklist file.'
    echo -e $BLACKLIST_CONTENT >>/etc/modprobe.d/blacklist.conf
    echo -e '------------------------- Done.\n'
    sleep 5

    echo '------------------------- Update system kernel.'
    sudo update-initramfs -u
    echo -e '------------------------- Done.\n'
    sleep 5

    read -p 'Now you need to restart your computer. Do you want to continue?[Y/n]' rebootComputerToContinue
    case $rebootComputerToContinue in
        [yY]*)
            echo '------------------------- Ready to restart your computer.'
            sudo reboot
            ;;
        [nN]*)
            outputErrorMsg "The operation was aborted, exit!"
            exit
            ;;
        *)
            outputErrorMsg "Illegal parameters, exit!"
    esac
fi

echo '------------------------- Add PPA repository of NVIDIA graphic driver.'
sudo add-apt-repository ppa:graphics-drivers/ppa
echo -e '------------------------- Done.\n'
sleep 5

updateSoftwareRepository

echo '------------------------- Uninstall old NVIDIA graphic driver.'
sudo apt-get remove --purge nvidia*
echo -e '------------------------- Done.\n'
sleep 5

echo '------------------------- Search for the current version of NVIDIA graphic driver.'
nvidiaCurrentVersion=`ubuntu-drivers devices | grep 'nvidia-driver' | grep 'recommended' | grep -E -o "nvidia-driver-[0-9]{1,3}"`
if [[ -z "$nvidiaCurrentVersion" ]]; then
    # No output, the NVIDIA graphic driver is query failure or haven't NVIDIA graphics card hardware.
    outputErrorMsg "The NVIDIA graphic driver is query failure or haven't NVIDIA graphics card hardware, exit!"
    exit
fi
echo -e "------------------------- Done. The current version is "$nvidiaCurrentVersion". \n"
sleep 5

echo "------------------------- Install NVIDIA graphic driver version is $nvidiaAppropriateVersion."
sudo apt-get install $nvidiaAppropriateVersion
echo -e '------------------------- Done.\n'
sleep 5

outputSuccessMsg "Install NVIDIA graphic driver(version:$$nvidiaAppropriateVersion) is complete!"

read -p 'Now you need to restart your computer. Do you want to continue?[Y/n]' rebootComputerOfcomplete
case $rebootComputerOfcomplete in
    [yY]*)
        echo '------------------------- Ready to restart your computer.'
        sudo reboot
        ;;
    [nN]*)
        outputErrorMsg "The operation was aborted, exit!"
        exit
        ;;
    *)
        outputErrorMsg "Illegal parameters, exit!"
esac
# ######################### end of install NVIDIA graphic driver #########################